<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="来源渠道" prop="channelCode">
        <el-select v-model="queryParams.channelCode" placeholder="请选择来源渠道" clearable>
          <el-option
            v-for="dict in channelList"
            :key="dict.channelCode"
            :label="dict.channelName"
            :value="dict.channelCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="platformUserId">
        <el-input
          v-model="queryParams.platformUserId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
       <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in user_logoff_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker clearable
          v-model="searchDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmSearchDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['user:CancelRecord:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="list">
        <el-table-column label="来源渠道" align="center" prop="channelName" min-width="150" show-overflow-tooltip />
        <el-table-column label="用户编号" align="center" prop="platformUserId" width="150" show-overflow-tooltip />
        <el-table-column label="用户名" align="center" prop="name" />
        <el-table-column label="手机号" align="center" prop="mobile" width="120" />
        <el-table-column label="电子卡号" align="center" prop="cardNo" width="120" />
        <el-table-column label="退款金额（元）" align="center" prop="balance" width="120">
          <template #default="scope">
            <!-- balance实退金额  amount可退金额 -->
            <span>{{ formatAmount(scope.row.auditStatus == 2 ? scope.row.balance : scope.row.amount) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="auditStatus" width="100">
          <template #default="scope">
            <dict-tag :options="user_logoff_status" :value="scope.row.auditStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" prop="createTime" width="160" />
        <el-table-column label="审核时间" align="center" prop="auditTime" width="160" />
        <el-table-column label="审核备注" align="center" prop="auditMsg" min-width="180" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80" fixed="right">
          <template #default="scope">
            <el-tooltip content="审核" placement="top">
              <div>
                <el-button v-if="scope.row.auditStatus == 1" link type="primary" icon="Edit" @click="handleApproval(scope.row)" v-hasPermi="['user:CancelRecord:edit']"></el-button>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="showDialog" title="注销审核" width="500px">
      <edit-approval 
        ref="editApproval"
        :status-list="user_logoff_status"
        @cancel="cancel"
        @confirm="confirm"
      />
    </Dialog>
  </div>
</template>

<script setup name="Account">
import { listCancelRecord } from "@/api/order/logoffList"
import { listChannel } from "@/api/channel/platchannelinfo"
import EditApproval from './edit.vue'
import { nextTick } from "vue"

const { proxy } = getCurrentInstance()
const { user_logoff_status }  = proxy.useDict('user_logoff_status')

const editApproval = ref()
const list = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const channelList = ref([])
const searchDate = ref([])
const showDialog = ref(false)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    platformUserId: null,
    mobile: null,
    channelCode: null,
    auditStatus: null,
    beginTime: null,
    endTime: null,
    orderType: 1,     // 按createTime倒序排列
  }
})

const { queryParams } = toRefs(data)

const confirmSearchDate = (e) => {
  if (e && e.length) {
    queryParams.value.beginTime = e[0]
    queryParams.value.endTime = e[1]
  } else {
    queryParams.value.beginTime = null
    queryParams.value.endTime = null
  }
  handleQuery()
}

/** 查询渠道列表 */
function getChannelList() {
  const params = {
    pageNum: 1,
    pageSize: 100
  }
  listChannel(params).then(response => {
    channelList.value = response.rows
  })
}

/** 查询注销记录列表 */
function getList() {
  loading.value = true
  listCancelRecord(queryParams.value).then(response => {
    list.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
}

function confirm() {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  searchDate.value = []
  queryParams.value.beginTime = null
  queryParams.value.endTime = null
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 审核操作 */
function handleApproval(row) {
  showDialog.value = true
  nextTick(() => {
    editApproval.value.pageInit(row)
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('user/cancelRecord/export', {
    ...queryParams.value,
    exportType: 'xhsh'
  }, `销户审核_${proxy.getFileImportName()}.xlsx`)
}

getChannelList()
getList()
</script>
